class ActivityController < ApplicationController
  def cognizance #审理活动帖
    @activities = Activity.find(:all,
      :conditions=> "visible=0",
      :limit=>5)
    @page = 1
    render :file => "#{RAILS_ROOT}/app/views/activity/cognizance.js.rjs"
  end

  def show
    @activity = Activity.find(params[:id])
  end

  def agree
    @activity = Activity.find(params[:id])
    @activity.visible = 1
    @activity.state = "招募中"
    @activity.save

    @topic = Topic.find( params[:id] )
    @topic.visible = 1
    @topic.save
    
    @page = params[:page].to_i
    @activities = Activity.find(:all,
      :conditions=> "visible=0",
      :limit=>5,
      :offset=>5*(@page-1))

    render :file => "#{RAILS_ROOT}/app/views/activity/agree.js.rjs"

  end

  def refuse
    Activity.delete(params[:id])
    Topic.delete(params[:id])

    @page = params[:page].to_i
    @activities = Activity.find(:all,
      :conditions=> "visible=0",
      :limit=>5,
      :offset=>5*(@page-1))

    render :file => "#{RAILS_ROOT}/app/views/activity/refuse.js.rjs"
  end

  def next_page
    @page = params[:page].to_i + 1
    @activities = Activity.find(:all,
      :conditions=> "visible=0",
      :limit=>5,
      :offset=>5*(@page-1))
    render :file => "#{RAILS_ROOT}/app/views/activity/change_page.js.rjs"
  end

    def last_page
    @page = params[:page].to_i - 1
    @activities = Activity.find(:all,
      :conditions=> "visible=0",
      :limit=>5,
      :offset=>5*(@page-1))
    render :file => "#{RAILS_ROOT}/app/views/activity/change_page.js.rjs"
  end

  def enroll #报名
    @user = User.find( session[:user_id] )
    @volunteer = Volunteer.find_by_account( @user.account )
    @activity = Activity.find(params[:id])

    if @activity.people_demande > 0
      @activity.volunteers << @volunteer
      @activity.people_demande = @activity.people_demande - 1
      if @activity.save
        render :file => "#{RAILS_ROOT}/app/views/activity/enroll_success.js.rjs"
      end
    else
      render :file => "#{RAILS_ROOT}/app/views/activity/enroll_fail.js.rjs"
    end
  end

  def close_activity
    @activity = Activity.find(params[:id])
    @volunteers = @activity.volunteers
    @volunteers.each do |volunteer|
      temp_volunteer = Volunteer.find( volunteer.id )
      temp_volunteer.service_time = volunteer.service_time + @activity.service_times
      #vomunteer.activities.delete @activity 做志愿者的活动记录都好
      temp_volunteer.save
    end
    @activity.state = "已结束"
    @activity.volunteers.clear
    @activity.save
    redirect_to :action=>"my_activities"
  end


  def my_activities
    @my_topics = Topic.find_all_by_user_id( session[:user_id], :order=>" created_at desc " )
    @my_activities = []
    @my_topics.each do |topic|
      if activity = Activity.find_by_id( topic.id )
        @my_activities << activity
      end
    end
    render :file => "#{RAILS_ROOT}/app/views/activity/my_activities.js.rjs"
  end

  def new_activity
    @error_msg = []
  end

  def create
    @error_msg = []
    if params[:title]==""
      @error_msg << "标题不能为空"
    end
    if params[:topic_content] == ""
      @error_msg << "活动详情不能为空"
    end
    if params[:people_demande]== "" || params[:people_demande].to_i < 0
      @error_msg << "需求人数必须为大于0的数字"
    end
    if params[:service_time]== "" || params[:service_time].to_i < 0
      @error_msg << "服务时必须为大于0的数字"
    end

    input_time = Time.gm(params[:deadline][:"written_on(1i)"].to_i,
      params[:deadline][:"written_on(2i)"].to_i,
      params[:deadline][:"written_on(3i)"].to_i)
    if  input_time < Time.now
      @error_msg << "截止日期必须大于今天"
    end

    if @error_msg.count > 0
      render :action=>"new_activity"
      return
    end

    @topic = Topic.new
    @activity = Activity.new

    @topic.title = params[:title]
    @topic.topic_content = params[:topic_content]
    @topic.user_id = session[:user_id]
    @topic.visible = 0
    @topic.save

    temp = Topic.find_by_title( @topic.title )

    @activity.id = temp.id
    @activity.deadline = input_time
    @activity.service_times = params[:service_time]
    @activity.people_demande = params[:people_demande]
    @activity.state = "未评审"
    @activity.visible = 0
    @activity.save

    redirect_to :controller=>"topics", :action=>"show", :id=>@activity.id

  end

end
